/**
 * Created by Administrator on 2017/4/20.
 */


var COLOR_CARD_NUM = 13;

var Color = {
    'SPEC': 0,           // 特殊牌
    'HEART': 1,          // 红桃
    'SPADE': 2,          // 黑桃
    'DIAMOND': 3,        // 方块
    'CLUB': 4            // 梅花
}

var SpecPoint = {
    'ANY': 1,               // 赖子
    'JOKER_S': 2,          // 小鬼
    'JOKER_B': 3,          // 大鬼
}

var Value = {};
Value.ANY = SpecPoint.ANY;
Value.JOKER_S = SpecPoint.JOKER_S;
Value.JOKER_B = SpecPoint.JOKER_B;
Value.HEART_A = 101;
Value.SPADE_A = 201;
Value.DIAMOND_A = 301;
Value.CLUB_A = 401;

var Point = {
    'J': 11,
    'Q': 12,
    'K': 13,
    'A': 1,

    "MIN": 2,
    "MAX": 13
};

// 根据花色和点数 计算牌数值
function getValue(color, point) {
    return color*100 + point;
}

// 根据牌数值 计算点数
function getPoint(value) {
    return value % 100;
}

// 根据牌数值 计算花色
function getColor(value) {
    return Math.floor(value/100);
}

function isBasicColor(color) {
    return color != Color.SPEC;
}

// 是否是特殊牌
function isSpecValue(value) {
    return value < 100;
}

function buildFromArray(cardsArray) {
    var cards = {};
    cardsArray.forEach(function(card) {
        ExObject.addNumber(cards, card, 1);
    });
    return cards;
}

function buildPointObjectFromArray(cardsArray) {
    var points = {};
    cardsArray.forEach(function(card) {
        ExObject.addNumber(points, getPoint(card), 1);
    });
    return points;
}

function isSameCards(cards1, cards2) {
    var keys1 = Object.keys(cards1);
    var keys2 = Object.keys(cards2);

    if (keys1.length != keys2.length) {
        return false;
    }

    var isSame = true;
    for (var i = 0, l  = keys1.length; i < l; ++i) {
        if (cards1[keys1[i]] != cards2[keys1[i]]) {
            isSame = false; break;
        }
    }

    return isSame;
}

/**
 * 计算牌组中牌的张数
 * @param cards
 * @returns {*}
 */
function sumCardsNum(cards) {
    return ExObject.sumValue(cards);
}

function existCard(cards, card) {
    return cards[card] > 0;
}

/**
 * 判断牌级中是否有指定花色的牌
 * @param cards
 * @param color
 * @returns {boolean}
 */
function existColor(cards, color) {
    var exist = false;
    ExObject.each(cards, function(card, num) {
        if (getColor(card) == color) {
            exist = true; return false;
        }
    })
    return exist;
}

function delCard(cards, card, delNum) {
    var num = cards[card] || 0;
    delNum = delNum || 1;

    if (num < delNum) {
        return false;
    } else if (num == delNum) {
        delete cards[card];
    } else {
        cards[card] = num - delNum;
    }
    return true;
}

function addCard(cards, card, num) {
    ExObject.addNumber(cards, card, num);
}

/**
 * 从牌组中获取指定花色的子牌组
 * @param cards
 * @returns {}
 */
function getColorCards(cards, color) {
    var colorCards = {};
    for (var card = this.getValue(color, 1), max = card + ColorCardNum[color].point; card < max; ++card) {
        var num = cards[card];
        if (num) {
            colorCards[card] = num;
        }
    }
    return colorCards;
}

module.exports = {
    COLOR_CARD_NUM: COLOR_CARD_NUM,

    Color: Color,
    SpecPoint: SpecPoint,
    Value: Value,
    Point: Point,

    getValue: getValue,
    getPoint: getPoint,
    getColor: getColor,
    isBasicColor: isBasicColor,
    isSpecValue: isSpecValue,

    buildFromArray: buildFromArray,
    buildPointObjectFromArray: buildPointObjectFromArray,
    isSameCards: isSameCards,
    sumCardsNum: sumCardsNum,
    existCard: existCard,
    existColor: existColor,
    delCard: delCard,
    addCard: addCard,
    getColorCards: getColorCards,
}